package com.mano.mall.web.shop.portal.controller;

import com.mano.commons.web.base.BaseWebController;
import com.mano.commons.web.constants.WebConstants;
import com.mano.commons.web.utils.CookieUtils;
import com.mano.mall.api.dto.member.output.UserOutputDTO;
import com.mano.mall.common.base.BaseResponse;
import com.mano.mall.web.shop.member.feign.MemberServiceFeign;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @Author: zj
 * @Description:
 * @Date: Created in 13:17 2020/9/27
 * @Modified By:
 */
@Controller
public class IndexController extends BaseWebController {

    private static final String INDEX_FTL="index";

    @Autowired
    private MemberServiceFeign memberServiceFeign;

    @RequestMapping("/")
    public String index(HttpServletRequest request, HttpServletResponse response, Model model){
        // 1.从cookie 中 获取 会员token
        String token = CookieUtils.getCookieValue(request, WebConstants.LOGIN_TOKEN_COOKIES);
        if(!StringUtils.isBlank(token)){
            BaseResponse<UserOutputDTO> baseResponse = memberServiceFeign.getInfo(token);
            if(isSuccess(baseResponse)){
                UserOutputDTO outputDTO = baseResponse.getData();
                if(Objects.nonNull(outputDTO)){
                    String mobile = outputDTO.getMobile();
                    // 对手机号码实现脱敏
                    String desensMobile = mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                    model.addAttribute("desensMobile", desensMobile);
                }
            }
        }


        return INDEX_FTL;
    }

    /**
     * 跳转到首页
     *
     * @return
     */
    @RequestMapping("/index.html")
    public String indexHtml() {
        return null;
    }

}
